/**
 * 日期组件对应公共数据
 */
export default {
  data() {
    return {
      showContrastSelect: false, // 动态时间筛选
      showContrastTime: false, // 时间范围选择
      filterSelectTime: {} // 数据筛选中的条件
    };
  },
  computed: {
    contrastTimeOptions() {
      const timeRangeType = this.filterSelectTime.timeRangeType || '';
      // { id: 'day', name: '今日' },
      // { id: 'week', name: '本周' },
      // { id: 'month', name: '本月' },
      // { id: 'year', name: '本年' }
      let arr = [];
      switch (timeRangeType) {
        case 'day':
          arr = [{ id: '昨日', name: '昨日' }, { id: '上周今日', name: '上周今日' }];
          break;
        case 'week':
          arr = [{ id: '上周', name: '上周' }];
          break;
        case 'month':
          arr = [{ id: '上月', name: '上月' }, { id: '去年本月', name: '去年本月' }];
          break;
        case 'year':
          arr = [{ id: '去年', name: '去年' }];
          break;
        default:
          arr = [];
          break;
      }
      if (arr && arr.length > 0) {
        this.$set(this.dataForm, 'contrastTime', arr[0].id || '');
      } else {
        this.$set(this.dataForm, 'contrastTime', '');
      }
      return arr;
    }
  },
  methods: {
    contrastChange() {
      if (this.dataForm.type !== 13) {
        this.showContrastSelect = false;
        this.showContrastTime = false;
        this.$set(this.dataForm, 'contrastTime', '');
        return;
      }
      const chartFilter = this.dataForm.chartFilter || [];
      let systemFilter = [];
      if (chartFilter && chartFilter.length > 0 && chartFilter[0].system) {
        systemFilter = chartFilter[0].system;
      }
      const xAxis = this.dataForm.xAxis || '';
      const selectTime = systemFilter.filter(i => {
        return i.field === xAxis;
      });
      if (selectTime.length > 0) {
        const timeRangeType = selectTime[0].timeRangeType;
        this.filterSelectTime = selectTime[0];
        if (timeRangeType && timeRangeType !== '') {
          this.showContrastSelect = true;
          this.showContrastTime = false;
          return;
        } else if (selectTime[0].begin && selectTime[0].end) {
          this.showContrastSelect = false;
          this.showContrastTime = true;
          this.$set(this.dataForm, 'contrastTime', '');
          return;
        }
      }
      this.showContrastSelect = false;
      this.showContrastTime = false;
      this.$set(this.dataForm, 'contrastTime', '');
    },
    handelParams(params) {
      if (params.contrastTime && params.contrastTime !== '') {
        params.contrastTimeRange = [];
      }
      // 如果是时间字段，则把运算符设为 =
      if (params.chartFilter && params.chartFilter.length > 0) {
        params.chartFilter.forEach(i => {
          if (i.system && i.system.length > 0) {
            i.system.forEach(j => {
              if (this.isTimeField(j) && j.begin && j.end && j.operation === '') {
                j.operation = '=';
              }
            });
          }
        });
      }
      // 如果图表不是折线对比图，那么不能进行对比，去除相关参数
      if (params.type !== 13) {
        params.contrastTimeRange = [];
        params.contrastTime = '';
      }
      if (!this.showContrastSelect) {
        params.contrastTime = '';
      }
      if (!this.showContrastTime) {
        params.contrastTimeRange = [];
      }
      if (params.type === 13 && typeof params.yAxis === 'string') {
        params.yAxis = params.yAxis ? [params.yAxis] : [];
      }
      return params;
    },
    isTimeField(item) {
      return !!(item && item.optionsType === 1);
    }
  }
};
